//
//  BBShopCollectionViewCell.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/10.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
import SDWebImage

class BBShopCollectionViewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var product:BBProduct? {
        didSet{
            nameLabel.text = product?.productName
            desLabel.text = product?.subName
            let url = URL.init(string: (product?.productImages) ?? "")
            productImage.sd_setImage(with: url, placeholderImage: UIImage(named: "default_image"), options:SDWebImageOptions.init(rawValue: 1), completed: nil)
            priceLabel.text = "€\(product!.price ?? 0).00"
        }
    }
    
    private func setupUI(){
        //添加子控件掉contentView上
        contentView.addSubview(cellBackView)
        cellBackView.addSubview(productImage)
        cellBackView.addSubview(nameLabel)
        cellBackView.addSubview(desLabel)
        cellBackView.addSubview(priceLabel)
        
        cellBackView.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size: CGSize(width:self.bounds.size.width-10, height: self.bounds.size.height-10), offset: CGPoint(x: 10, y: 10))
        productImage.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: cellBackView, size: CGSize(width: self.bounds.size.width-20, height: (self.bounds.size.height-10)/2), offset: CGPoint(x: 5, y: 5))
        nameLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: productImage, size: CGSize(width: self.bounds.size.width-20, height: 20), offset: CGPoint(x: 0, y: 5))
        desLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: nameLabel, size: CGSize(width: self.bounds.size.width-20, height: 50), offset: CGPoint(x: 0, y: 5))
        priceLabel.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: cellBackView, size: CGSize(width: 100, height: 20), offset: CGPoint(x: -10, y: -5))
    }
    
    private lazy var productImage:UIImageView = {
        let iv = UIImageView()
        iv.image = UIImage(named: "default_product")
        return iv
    }()
    
    private lazy var nameLabel:UILabel = {
        let la = UILabel()
        la.textColor = UIColor.darkGray
        return la
    }()
    
    private lazy var desLabel:UILabel = {
        let la = UILabel()
        la.font = UIFont.systemFont(ofSize: 12)
        la.textColor = UIColor.lightGray
        la.numberOfLines = 0
        return la
    }()
    
    private lazy var priceLabel:UILabel = {
       let la = UILabel()
       la.textAlignment = NSTextAlignment.right
       return la
    }()
    
    private lazy var cellBackView:UIView = {
        let ve = UIView()
        ve.backgroundColor = UIColor(red: 249.0/255.0, green: 245.0/255.0, blue: 237.0/255.0, alpha: 1)
        return ve
    }()
    
}
